<?php
/**
 * Created by PhpStorm.
 * User: liaozhenhao
 * Date: 2017/12/13
 * Time: 下午5:34
 */

namespace app\wx\controller;


use phpDocumentor\Reflection\Types\Null_;

class Ams
{
    var $app_id='ams';

    function data_call_back()
    {
        $wxcpt=model('WXBizMsgCrypt');
        $config=config('wx');
        $wxcpt->set_config($config[$this->app_id.'_Token'],$config[$this->app_id.'ams_EncodingAESKey'],$config['corpid']);

        $sVerifyMsgSig = input('get.msg_signature');
        $sVerifyTimeStamp = input('get.timestamp');
        $sVerifyNonce = input('get.nonce');
        $sVerifyEchoStr = input('get.echostr');

        $sEchoStr='';
        $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
        if ($errCode == 0)
        {
            //
            // 验证URL成功，将sEchoStr返回
            echo $wxcpt->sEchoStr;
            exit(0);
        } else {
            print("ERR: " . $errCode . "\n\n");
        }
    }
    public function receive_ticket()
    {
        //初始化
        $wxcpt=model('WXBizMsgCrypt');
        $config=config('wx');
        $wxcpt->set_config($config[$this->app_id.'_Token'],$config[$this->app_id.'_EncodingAESKey'],$config[$this->app_id.'_SuiteID']);


        //获取企业微信推送的信息，get
        $sReqMsgSig = input("get.msg_signature");
        $sReqTimeStamp = input("get.timestamp");
        $sReqNonce = input("get.nonce");
        //获取企业微信推送的xml，post
        $sReqData = file_get_contents("php://input");

        //把提交的信息解密处理
        $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg='');

        if ($errCode == 0)
        {

            $xml = new \DOMDocument();
            $xml->loadXML($wxcpt->sMsg);

            $InfoType = $xml->getElementsByTagName('InfoType')->item(0)->nodeValue;
            //更新企业微信推送的SuiteTicket
            if($InfoType=='suite_ticket')
            {
                $suiteticket = $xml->getElementsByTagName('SuiteTicket')->item(0)->nodeValue;
                db('suite_config')->where('app',$this->app_id)->update(['suiteticket'=>$suiteticket]);
            }

            //记录请求的时间
            $TimeStamp=$xml->getElementsByTagName('TimeStamp')->item(0)->nodeValue;
            if(session('timestamp')==$TimeStamp&&session('timestamp')!='')
            {
                echo 'success';
                exit;
            }else{
                session('timestamp',$TimeStamp);
            }

            db('back_call')->insert(['content'=>$wxcpt->sMsg]);

            //如果推送的信息为安装套件
            if($InfoType=='create_auth')
            {
                //得到临时授权码，获取永久授权码
                $AuthCode = $xml->getElementsByTagName('AuthCode')->item(0)->nodeValue;
                $res=model('Qywx')->get_permanent_code($this->app_id,$AuthCode);
                $json=json_decode($res,true);
                if($json['access_token']=='')
                {
                    print("ERR: ".$json['errcode']."\n\n");
                    exit;
                }
                //把得到的授权企业注册成为企业用户
                $add_res=model('Reg')->add_company($this->app_id,$json);
                if($add_res['res']!=true)
                {
                    print("ERR: ".$add_res['msg']."\n\n");
                    exit;
                }
                session('timestamp',null);
            }

            //企业取消授权推送
            if($InfoType=='cancel_auth')
            {
                $AuthCorpId = $xml->getElementsByTagName('AuthCorpId')->item(0)->nodeValue;
                model('Reg')->cancel_company($AuthCorpId);
                session('timestamp',null);
            }

            //变更事件推送
            if($InfoType=='change_contact')
            {
                $ChangeType = $xml->getElementsByTagName('ChangeType')->item(0)->nodeValue;
                if($ChangeType=='create_party'||$ChangeType=='update_party'||$ChangeType=='delete_party')
                {
                    $AuthCorpId = $xml->getElementsByTagName('AuthCorpId')->item(0)->nodeValue;
                    //事件为创建部门
                    model('Reg')->copy_wx_dept($this->app_id,$AuthCorpId);
                }
                if($ChangeType=='create_user')
                {
                    //创建成员用户
                    model('Reg')->create_user($wxcpt->sMsg);
                }
                if($ChangeType=='update_user')
                {
                    //编辑成员用户
                    model('Reg')->update_user($wxcpt->sMsg);
                }
                if($ChangeType=='delete_user')
                {
                    //删除成员用户
                    model('Reg')->delete_user($wxcpt->sMsg);
                }
                session('timestamp',null);
            }

            echo 'success';
        } else {
            db('back_call')->insert(['content'=>$errCode]);
            print("ERR: " . $errCode . "\n\n");

        }
    }
}